﻿package action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import model.Choice;
import model.Fill;
import model.Judge;
import model.PickedChoice;
import model.PickedFill;
import model.PickedJudge;
import model.Result;
import model.Student;
import model.Test;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dao.BasicDao;
import dao.ChoiceDAO;
import dao.FillDAO;
import dao.JudgeDAO;
import dao.ResultDAO;
import dao.TestDAO;

@SuppressWarnings("serial")
public class StudentAction extends ActionSupport{
	BasicDao basicDao=new BasicDao();
	// 登录
	private String username;
	private String password;
	//选择考试
	private List<Object> tests=null;
	private List<String> dates=new ArrayList<String>();
	//生成试题
	private int testId;
	private List<Choice> choices=new ArrayList<Choice>();
	private List<Fill> fills=new ArrayList<Fill>();
	private List<Judge> judges=new ArrayList<Judge>();
	private ChoiceDAO choiceDAO=new ChoiceDAO();
	private FillDAO fillDAO=new FillDAO();
	private JudgeDAO judgeDAO=new JudgeDAO();
	private TestDAO testDAO=new TestDAO();
	//判卷
	private List<Integer> choiceIds;
	private List<Integer> judgeIds;
	private List<Integer> fillIds;
	private ResultDAO resultDAO=new ResultDAO();
	//提示信息
	private String message;
	
	public List<Integer> getChoiceIds() {
		return choiceIds;
	}
	public void setChoiceIds(List<Integer> choiceIds) {
		this.choiceIds = choiceIds;
	}
	public List<Integer> getJudgeIds() {
		return judgeIds;
	}
	public void setJudgeIds(List<Integer> judgeIds) {
		this.judgeIds = judgeIds;
	}
	public List<Integer> getFillIds() {
		return fillIds;
	}
	public void setFillIds(List<Integer> fillIds) {
		this.fillIds = fillIds;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setTests(List<Object> tests) {
		this.tests = tests;
	}
	public List<Object> getTests() {
		return tests;
	}
	public String login() {
		List<Object> studentList=basicDao.getObjectByName("Student", username);
		if (studentList!=null&&studentList.size()!=0) {
			Student student = (Student)studentList.get(0);
			if (student.getPwd().equals(password)) {
				Map session = ServletActionContext.getContext().getSession();
				session.put("student", student);
				setTests(basicDao.getAllObjects("Test"));
				for (int i = 0; i < tests.size(); i++) {
					Test test=(Test) tests.get(i);
					Timestamp timestamp=test.getDate();
					String temp="";
					SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
					temp=format2.format(timestamp);
					getDates().add(temp);
				}
				return SUCCESS;
			}else {
				return "failure";
			}
		} else {
			return "failure";
		}
	}
	public String exam() {
		Test test=testDAO.getTestById("Test", testId);
		boolean tested=false;
//		Date date=new Date();
//		System.out.println(date);
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String temp = format.format(date);
//		Timestamp timestamp = Timestamp.valueOf(temp);
		Timestamp now = new Timestamp(System.currentTimeMillis());
		long now_num=now.getTime();
		long test_num=test.getDate().getTime();
		if (now_num<test_num) {
			return "failure";
		}
		
		Map session = ActionContext.getContext().getSession();
		Student student = (Student)session.get("student"); //c从session中获取student信息
		String studentName=student.getName();
		
		//判断是否已经考过此试卷
		List<Object> results=resultDAO.getResultsByTest(testId);
		for (int i = 0; i < results.size(); i++) {
			Result result=(Result) results.get(i);
			if (result.getSId()==Integer.parseInt(studentName)) {
				 tested=true;
			}
		}
		
		//进入考试
		if (!tested) {
			List<PickedChoice> pickedChoices=testDAO.getQuestionsByTestId("PickedChoice", testId);
			for (int i = 0; i < pickedChoices.size(); i++) {
				int choiceId=pickedChoices.get(i).getChoiceId();
				choices.add(choiceDAO.changeOption( choiceDAO.getChoiceById("Choice",choiceId),studentName));
				
			}
			
			List<PickedFill> pickedFills=testDAO.getQuestionsByTestId("PickedFill", testId);
			for (int i = 0; i < pickedFills.size(); i++) {
				int fillId=pickedFills.get(i).getFillId();
				fills.add(fillDAO.getFillById("Fill", fillId));
			}
			
			List<PickedJudge> pickedJudges=testDAO.getQuestionsByTestId("PickedJudge", testId);
			for (int i = 0; i < pickedFills.size(); i++) {
				int judgeId=pickedFills.get(i).getFillId();
			judges.add(judgeDAO.getJudgeById("Judge", judgeId));
			}
			
			int totalPoint=test.getCNum()*test.getCValue()+test.getFNum()*test.getFValue()+test.getJNum()			*test.getJValue();
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("choices", choices);
			request.setAttribute("fills", fills);
			request.setAttribute("judges", judges);
			request.setAttribute("test", test);
			request.setAttribute("totalPoint", totalPoint);
			return SUCCESS;
		}else {
			//addActionError("您已经参加过次考试");
			//setMessage("您已经参加过次考试");
			return ERROR;		
		}
		
	}
	public int getTestId() {
		return testId;
	}
	public void setTestId(int testId) {
		this.testId = testId;
	}
	public List<Choice> getChoices() {
		return choices;
	}
	public void setChoices(List<Choice> choices) {
		this.choices = choices;
	}
	public List<Fill> getFills() {
		return fills;
	}
	public void setFills(List<Fill> fills) {
		this.fills = fills;
	}
	public List<Judge> getJudges() {
		return judges;
	}
	public void setJudges(List<Judge> judges) {
		this.judges = judges;
	}
	
	public String judge_paper() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Test test=testDAO.getTestById("Test", testId);
		List<String> choiceAnswers = new ArrayList<String>();
		List<String> fillAnswers = new ArrayList<String>();
		List<String> judgeAnswers = new ArrayList<String>();
		for(int i = 0; i < 20; i++) {
			String answer = request.getParameter("choiceAnswer"+i);
			choiceAnswers.add(answer);
		}
		for(int i = 0; i < 20; i++) {
			String answer = request.getParameter("judgeAnswer"+i);
			judgeAnswers.add(answer);
		}
		for(int i = 0; i < 20; i++) {
			String answer = request.getParameter("fillAnswer"+i);
			fillAnswers.add(answer);
		}
		int choicePoint = choiceDAO.accountChoiceResult(choiceIds, choiceAnswers, test.getCValue());
		int judgePoint = judgeDAO.accountJudgeResult(judgeIds, judgeAnswers, test.getJValue());  
		int fillPoint = fillDAO.accountFillResult(fillIds, fillAnswers, test.getFValue());

		
		
		
		//设置成绩到学生信息中
		Map session = ActionContext.getContext().getSession();
		Student student = (Student)session.get("student"); //c从session中获取student信息		
		String studentName = student.getName();
		Result result=new Result();
		result.setSId(Integer.parseInt(studentName));
		result.setTestId(testId);
		int score=choicePoint+judgePoint+fillPoint;
		result.setScore(score);
		resultDAO.saveResult(result);
		
		request.setAttribute("studentName", studentName);//保存学生姓名和总分数
		request.setAttribute("choicePoint", choicePoint);
		request.setAttribute("fillPoint", fillPoint);
		request.setAttribute("judgePoint", judgePoint);
		request.setAttribute("totalPoint", score);
		setMessage("交卷成功");
		return SUCCESS;
	}
	public void setDates(List<String> dates) {
		this.dates = dates;
	}
	public List<String> getDates() {
		return dates;
	}
	public String list_test() {
		setTests(basicDao.getAllObjects("Test"));
		for (int i = 0; i < tests.size(); i++) {
			Test test=(Test) tests.get(i);
			Timestamp timestamp=test.getDate();
			String temp="";
			SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
			temp=format2.format(timestamp);
			getDates().add(temp);
		}
		return SUCCESS;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getMessage() {
		return message;
	}
}
